<?php

namespace app\api\controller\v1;

use app\admin\model\BdlCarouselchart;
use app\admin\model\Materialcate;
use app\admin\model\Materialentry;
use app\admin\model\Matprop;
use app\admin\model\Surface;
use app\admin\model\Texture;
use think\Cache;

class Basic extends Base
{
    //分类
    public function getMaterialcate()
    {

            $cacheTag       = \config('cache_tag')['v1_basic_getMaterialcate'];
            $cacheKey       = $cacheTag. $this->datacenter . $this->forgid;
            $data         =  Cache::get($cacheKey);

        if(!$data)
        {

            $data = Materialcate::where('fenable', 1)
                ->whereIn('forgid', $this->forgids)
                ->where('fk_bdl_bd_datacenter', $this->datacenter)
                ->field('id,fname,fnumber,forgid,fk_bdl_bd_datacenter,fk_bdl_super,fenable')
                ->order('id','desc')
                ->select();

            $data = $this->buildTree($data->toArray());

            Cache::tag($cacheTag)->set($cacheKey,$data,$this->cacheExpire);

        }


        $this->success('获取分类成功', $data);
    }

    //属性
    public function getMatpop()
    {
        $materialCateId = $this->params['materialCateId'] ?? null;

        if (!$materialCateId) {
            $this->error('缺少材料分类ID');
        }

        $cacheTag       = \config('cache_tag')['v1_basic_getMatpop'];
        $cacheKey       = $cacheTag. $this->datacenter . $this->forgid . $materialCateId;
        $data         =  Cache::get($cacheKey);



        if(!$data)
        {
            $materialEntryIds = Materialentry::where('fk_bdl_bd_material_g', $materialCateId)
                ->order('fseq','ascs')
                ->select()
                ->each(function ($item)
                {
                    $son = Matprop::where('fenable', 1)
                        ->whereIn('id', $item->fk_bdl_bd_matprop)
                        ->field('id,fname,fnumber,forgid,fk_bdl_bd_datacenter,fenable,fk_bdl_unit')
                        ->whereIn('forgid', $this->forgids)
                        ->where('fk_bdl_bd_datacenter', $this->datacenter)
                        ->whereNotIn('fnumber', ['bdl_bd_texture', 'bdl_bd_surface'])
                        ->find();
                    $item->son = $son;
                });

            //组装
            $data =  [];
            foreach ($materialEntryIds as $k=>$v)
            {
                if(!empty($v['son']))
                {
                    $data[] = $v['son'];
                }
            }

            $data = json_decode(json_encode($data),true);
            $data = $this->removeDuplicateFnames($data);

            Cache::tag($cacheTag)->set($cacheKey,$data,$this->cacheExpire);
        }

        $this->success('获取属性成功', $data);
    }
    public function removeDuplicateFnames(&$array, $key = 'fname') {
        // 使用一个关联数组来存储唯一的fname和对应的子数组
        $uniqueArray = [];

        foreach ($array as $subArray) {
            $fname = $subArray[$key];

            // 检查fname是否已经在$uniqueArray中
            if (!isset($uniqueArray[$fname])) {
                // 如果不在，则添加到$uniqueArray中
                $uniqueArray[$fname] = $subArray;
            }
        }

        // 重新设置$array为唯一数组的值（只保留第一个出现的同名项）
        $array = array_values($uniqueArray);

        return $array;
    }
    //表面
    public function getSurface()
    {
        $cacheTag       = \config('cache_tag')['v1_basic_getSurface'];

        $map = [];

        $keyword = !empty($this->params['keyword']) ? trim($this->params['keyword']) : '';

        if (!empty($keyword)) {
            $map['fname'] = ['like', '%' . $keyword . '%'];
        }
        $cacheKey       = $cacheTag. $this->datacenter . $this->forgid . $keyword;
        $data         =  Cache::get($cacheKey);

        if(!$data)
        {
            $data = Surface::where('fenable', 1)
                ->whereIn('forgid', $this->forgids)
                ->where('fk_bdl_bd_datacenter', $this->datacenter)
                ->where($map)
                ->field('id,fname,fnumber,forgid,fk_bdl_bd_datacenter,fenable')
                ->order([
                    'fname'=>'a-z',
                    'fname'=>'0-9',
                ])
                ->select();

            Cache::tag($cacheTag)->set($cacheKey,$data,$this->cacheExpire);
        }



        $this->success('获取表面成功', $data);

    }
    //材质
    public function getTexture()
    {
        $cacheTag       = \config('cache_tag')['v1_basic_getTexture'];

        $map = [];

        $keyword = !empty($this->params['keyword']) ? trim($this->params['keyword']) : '';

        if (!empty($keyword)) {
            $map['fname'] = ['like', '%' . $keyword . '%'];
        }
        $cacheKey       = $cacheTag. $this->datacenter . $this->forgid . $keyword;
        $data         =  Cache::get($cacheKey);

        if(!$data)
        {
            $data = Texture::where('fenable', 1)
                ->whereIn('forgid', $this->forgids)
                ->where('fk_bdl_bd_datacenter', $this->datacenter)
                ->where($map)
                ->field('id,fname,fnumber,forgid,fk_bdl_bd_datacenter,fenable')
                ->order([
                    'fname'=>'a-z',
                    'fname'=>'0-9',
                ])
                ->select();
            Cache::tag($cacheTag)->set($cacheKey,$data,$this->cacheExpire);
        }




        $this->success('获取材质成功', $data);
    }



}